if exists (select 1
   from sys.sysreferences r join sys.sysobjects o on (o.id = r.constid and o.type = 'F')
   where r.fkeyid = object_id('AdviseInfo') and o.name = 'FK_ADVISEIN_REFERENCE_UPLOADFI')
alter table AdviseInfo
   drop constraint FK_ADVISEIN_REFERENCE_UPLOADFI
go

if exists (select 1
   from sys.sysreferences r join sys.sysobjects o on (o.id = r.constid and o.type = 'F')
   where r.fkeyid = object_id('AdviseInfo') and o.name = 'FK_ADVISEIN_REFERENCE_ADVISEDI')
alter table AdviseInfo
   drop constraint FK_ADVISEIN_REFERENCE_ADVISEDI
go

if exists (select 1
   from sys.sysreferences r join sys.sysobjects o on (o.id = r.constid and o.type = 'F')
   where r.fkeyid = object_id('ArticleActionLog') and o.name = 'FK_ARTICLEA_REFERENCE_ARTICLEI')
alter table ArticleActionLog
   drop constraint FK_ARTICLEA_REFERENCE_ARTICLEI
go

if exists (select 1
   from sys.sysreferences r join sys.sysobjects o on (o.id = r.constid and o.type = 'F')
   where r.fkeyid = object_id('ArticleActionLog') and o.name = 'FK_ARTICLEA_REFERENCE_USERS')
alter table ArticleActionLog
   drop constraint FK_ARTICLEA_REFERENCE_USERS
go

if exists (select 1
   from sys.sysreferences r join sys.sysobjects o on (o.id = r.constid and o.type = 'F')
   where r.fkeyid = object_id('ArticleActionLog') and o.name = 'FK_ARTICLEA_REFERENCE_ARTICLEA')
alter table ArticleActionLog
   drop constraint FK_ARTICLEA_REFERENCE_ARTICLEA
go

if exists (select 1
   from sys.sysreferences r join sys.sysobjects o on (o.id = r.constid and o.type = 'F')
   where r.fkeyid = object_id('ArticleCommentInfo') and o.name = 'FK_ARTICLEC_REFERENCE_USERS')
alter table ArticleCommentInfo
   drop constraint FK_ARTICLEC_REFERENCE_USERS
go

if exists (select 1
   from sys.sysreferences r join sys.sysobjects o on (o.id = r.constid and o.type = 'F')
   where r.fkeyid = object_id('ArticleCommentInfo') and o.name = 'FK_ARTICLEC_REFERENCE_ARTICLEI')
alter table ArticleCommentInfo
   drop constraint FK_ARTICLEC_REFERENCE_ARTICLEI
go

if exists (select 1
   from sys.sysreferences r join sys.sysobjects o on (o.id = r.constid and o.type = 'F')
   where r.fkeyid = object_id('ArticleHotMappingInfo') and o.name = 'FK_ARTICLEH_REFERENCE_ARTICLEI')
alter table ArticleHotMappingInfo
   drop constraint FK_ARTICLEH_REFERENCE_ARTICLEI
go

if exists (select 1
   from sys.sysreferences r join sys.sysobjects o on (o.id = r.constid and o.type = 'F')
   where r.fkeyid = object_id('ArticleHotMappingInfo') and o.name = 'FK_ARTICLEH_REFERENCE_ARTICLEH')
alter table ArticleHotMappingInfo
   drop constraint FK_ARTICLEH_REFERENCE_ARTICLEH
go

if exists (select 1
   from sys.sysreferences r join sys.sysobjects o on (o.id = r.constid and o.type = 'F')
   where r.fkeyid = object_id('ArticleInfo') and o.name = 'FK_ARTICLEI_REFERENCE_ARTICLET')
alter table ArticleInfo
   drop constraint FK_ARTICLEI_REFERENCE_ARTICLET
go

if exists (select 1
   from sys.sysreferences r join sys.sysobjects o on (o.id = r.constid and o.type = 'F')
   where r.fkeyid = object_id('ArticleInfo') and o.name = 'FK_ARTICLEI_REFERENCE_USERS')
alter table ArticleInfo
   drop constraint FK_ARTICLEI_REFERENCE_USERS
go

if exists (select 1
   from sys.sysreferences r join sys.sysobjects o on (o.id = r.constid and o.type = 'F')
   where r.fkeyid = object_id('ArticleRewardInfo') and o.name = 'FK_ARTICLER_REFERENCE_REWARDTY')
alter table ArticleRewardInfo
   drop constraint FK_ARTICLER_REFERENCE_REWARDTY
go

if exists (select 1
   from sys.sysreferences r join sys.sysobjects o on (o.id = r.constid and o.type = 'F')
   where r.fkeyid = object_id('ArticleRewardInfo') and o.name = 'FK_ARTICLER_REFERENCE_ARTICLEI')
alter table ArticleRewardInfo
   drop constraint FK_ARTICLER_REFERENCE_ARTICLEI
go

if exists (select 1
   from sys.sysreferences r join sys.sysobjects o on (o.id = r.constid and o.type = 'F')
   where r.fkeyid = object_id('ArticleRewardInfo') and o.name = 'FK_ARTICLER_REFERENCE_USERS')
alter table ArticleRewardInfo
   drop constraint FK_ARTICLER_REFERENCE_USERS
go

if exists (select 1
   from sys.sysreferences r join sys.sysobjects o on (o.id = r.constid and o.type = 'F')
   where r.fkeyid = object_id('FriendLinkInfo') and o.name = 'FK_FRIENDLI_REFERENCE_WEBSITEI')
alter table FriendLinkInfo
   drop constraint FK_FRIENDLI_REFERENCE_WEBSITEI
go

if exists (select 1
   from sys.sysreferences r join sys.sysobjects o on (o.id = r.constid and o.type = 'F')
   where r.fkeyid = object_id('UploadFileInfo') and o.name = 'FK_UPLOADFI_REFERENCE_WEBSITEI')
alter table UploadFileInfo
   drop constraint FK_UPLOADFI_REFERENCE_WEBSITEI
go

if exists (select 1
   from sys.sysreferences r join sys.sysobjects o on (o.id = r.constid and o.type = 'F')
   where r.fkeyid = object_id('UserRoles') and o.name = 'FK_USERROLE_REFERENCE_USERS')
alter table UserRoles
   drop constraint FK_USERROLE_REFERENCE_USERS
go

if exists (select 1
   from sys.sysreferences r join sys.sysobjects o on (o.id = r.constid and o.type = 'F')
   where r.fkeyid = object_id('UserRoles') and o.name = 'FK_USERROLE_REFERENCE_ROLES')
alter table UserRoles
   drop constraint FK_USERROLE_REFERENCE_ROLES
go

if exists (select 1
            from  sysobjects
           where  id = object_id('AdviseDisplayPosition')
            and   type = 'U')
   drop table AdviseDisplayPosition
go

if exists (select 1
            from  sysobjects
           where  id = object_id('AdviseInfo')
            and   type = 'U')
   drop table AdviseInfo
go

if exists (select 1
            from  sysobjects
           where  id = object_id('ArticleActionLog')
            and   type = 'U')
   drop table ArticleActionLog
go

if exists (select 1
            from  sysobjects
           where  id = object_id('ArticleActionType')
            and   type = 'U')
   drop table ArticleActionType
go

if exists (select 1
            from  sysobjects
           where  id = object_id('ArticleCommentInfo')
            and   type = 'U')
   drop table ArticleCommentInfo
go

if exists (select 1
            from  sysobjects
           where  id = object_id('ArticleHotInfo')
            and   type = 'U')
   drop table ArticleHotInfo
go

if exists (select 1
            from  sysobjects
           where  id = object_id('ArticleHotMappingInfo')
            and   type = 'U')
   drop table ArticleHotMappingInfo
go

if exists (select 1
            from  sysobjects
           where  id = object_id('ArticleInfo')
            and   type = 'U')
   drop table ArticleInfo
go

if exists (select 1
            from  sysobjects
           where  id = object_id('ArticleRewardInfo')
            and   type = 'U')
   drop table ArticleRewardInfo
go

if exists (select 1
            from  sysobjects
           where  id = object_id('ArticleTypeInfo')
            and   type = 'U')
   drop table ArticleTypeInfo
go

if exists (select 1
            from  sysobjects
           where  id = object_id('FriendLinkInfo')
            and   type = 'U')
   drop table FriendLinkInfo
go

if exists (select 1
            from  sysobjects
           where  id = object_id('RewardType')
            and   type = 'U')
   drop table RewardType
go

if exists (select 1
            from  sysobjects
           where  id = object_id('Roles')
            and   type = 'U')
   drop table Roles
go

if exists (select 1
            from  sysobjects
           where  id = object_id('UploadFileInfo')
            and   type = 'U')
   drop table UploadFileInfo
go

if exists (select 1
            from  sysobjects
           where  id = object_id('UserRoles')
            and   type = 'U')
   drop table UserRoles
go

if exists (select 1
            from  sysobjects
           where  id = object_id('Users')
            and   type = 'U')
   drop table Users
go

if exists (select 1
            from  sysobjects
           where  id = object_id('WebSiteInfo')
            and   type = 'U')
   drop table WebSiteInfo
go

/*==============================================================*/
/* Table: AdviseDisplayPosition                                 */
/*==============================================================*/
create table AdviseDisplayPosition (
   Id                   int                  identity,
   DisplayPositionName  nvarchar(80)         null,
   Section              nvarchar(80)         null,
   constraint PK_ADVISEDISPLAYPOSITION primary key (Id)
)
go

/*==============================================================*/
/* Table: AdviseInfo                                            */
/*==============================================================*/
create table AdviseInfo (
   Id                   int                  identity,
   Title                nvarchar(80)         null,
   UploadFileId         int                  null,
   DisplayPositionId    int                  null,
   Links                nvarchar(80)         null,
   AdviseSuplier        nvarchar(80)         null,
   constraint PK_ADVISEINFO primary key (Id)
)
go

/*==============================================================*/
/* Table: ArticleActionLog                                      */
/*==============================================================*/
create table ArticleActionLog (
   Id                   int                  identity,
   ArticleId            int                  null,
   ActionTypeId         int                  null,
   UserId               int                  null,
   ActionedTime         datetime             null,
   constraint PK_ARTICLEACTIONLOG primary key (Id)
)
go

/*==============================================================*/
/* Table: ArticleActionType                                     */
/*==============================================================*/
create table ArticleActionType (
   Id                   int                  identity,
   ActionTypeName       nvarchar(80)         null,
   constraint PK_ARTICLEACTIONTYPE primary key (Id)
)
go

/*==============================================================*/
/* Table: ArticleCommentInfo                                    */
/*==============================================================*/
create table ArticleCommentInfo (
   Id                   int                  identity,
   ArticleId            int                  null,
   Comment              nvarchar(2000)       null,
   UserId               int                  null,
   CommentedTime        datetime             null,
   constraint PK_ARTICLECOMMENTINFO primary key (Id)
)
go

/*==============================================================*/
/* Table: ArticleHotInfo                                        */
/*==============================================================*/
create table ArticleHotInfo (
   Id                   int                  identity,
   HotTypeName          nvarchar(80)         null,
   constraint PK_ARTICLEHOTINFO primary key (Id)
)
go

/*==============================================================*/
/* Table: ArticleHotMappingInfo                                 */
/*==============================================================*/
create table ArticleHotMappingInfo (
   Id                   int                  identity,
   ArticleId            int                  null,
   HotTypeId            int                  null,
   constraint PK_ARTICLEHOTMAPPINGINFO primary key (Id)
)
go

/*==============================================================*/
/* Table: ArticleInfo                                           */
/*==============================================================*/
create table ArticleInfo (
   Id                   int                  identity,
   Title                nvarchar(80)         null,
   ShortDesc            nvarchar(200)        null,
   Content              nvarchar(max)        null,
   AurthorId            int                  null,
   TypeId               int                  null,
   constraint PK_ARTICLEINFO primary key (Id)
)
go

/*==============================================================*/
/* Table: ArticleRewardInfo                                     */
/*==============================================================*/
create table ArticleRewardInfo (
   Id                   int                  identity,
   ArticleId            int                  null,
   RewardTypeId         int                  null,
   RewardUserId         int                  null,
   RewardAmount         decimal(18,2)        null,
   RewardedTime         datetime             null,
   constraint PK_ARTICLEREWARDINFO primary key (Id)
)
go

/*==============================================================*/
/* Table: ArticleTypeInfo                                       */
/*==============================================================*/
create table ArticleTypeInfo (
   Id                   int                  identity,
   TypeName             nvarchar(80)         null,
   IsActived            bit                  null,
   IsDeleted            bit                  null,
   CreatedTime          datetime             null,
   UpdatedTime          datetime             null,
   DisplayOrder         int                  null,
   Remarks              nvarchar(800)        null,
   constraint PK_ARTICLETYPEINFO primary key (Id)
)
go

/*==============================================================*/
/* Table: FriendLinkInfo                                        */
/*==============================================================*/
create table FriendLinkInfo (
   Id                   int                  identity,
   LinkName             nvarchar(80)         null,
   LinkTitle            nvarchar(80)         null,
   Links                nvarchar(80)         null,
   WebSiteId            int                  null,
   constraint PK_FRIENDLINKINFO primary key (Id)
)
go

/*==============================================================*/
/* Table: RewardType                                            */
/*==============================================================*/
create table RewardType (
   Id                   int                  identity,
   RewardTypeName       nvarchar(80)         null,
   constraint PK_REWARDTYPE primary key (Id)
)
go

/*==============================================================*/
/* Table: Roles                                                 */
/*==============================================================*/
create table Roles (
   Id                   int                  identity,
   RoleName             nvarchar(80)         null,
   constraint PK_ROLES primary key (Id)
)
go

/*==============================================================*/
/* Table: UploadFileInfo                                        */
/*==============================================================*/
create table UploadFileInfo (
   Id                   int                  identity,
   OriginName           nvarchar(80)         null,
   CurrentName          nvarchar(80)         null,
   RelativePath         nvarchar(80)         null,
   WebSiteId            int                  null,
   constraint PK_UPLOADFILEINFO primary key (Id)
)
go

/*==============================================================*/
/* Table: UserRoles                                             */
/*==============================================================*/
create table UserRoles (
   Id                   int                  identity,
   UserId               int                  null,
   RoleId               int                  null,
   constraint PK_USERROLES primary key (Id)
)
go

/*==============================================================*/
/* Table: Users                                                 */
/*==============================================================*/
create table Users (
   Id                   int                  identity,
   Username             nvarchar(80)         null,
   Password             nvarchar(80)         null,
   constraint PK_USERS primary key (Id)
)
go

/*==============================================================*/
/* Table: WebSiteInfo                                           */
/*==============================================================*/
create table WebSiteInfo (
   Id                   int                  identity,
   WebSiteName          nvarchar(80)         null,
   LogoFileId           int                  null,
   ShortDesc            nvarchar(800)        null,
   MasterName           nvarchar(80)         null,
   MasterTelephone      nvarchar(80)         null,
   ICPNo                nvarchar(80)         null,
   PublicSafeNo         nvarchar(80)         null,
   constraint PK_WEBSITEINFO primary key (Id)
)
go

alter table AdviseInfo
   add constraint FK_ADVISEIN_REFERENCE_UPLOADFI foreign key (UploadFileId)
      references UploadFileInfo (Id)
go

alter table AdviseInfo
   add constraint FK_ADVISEIN_REFERENCE_ADVISEDI foreign key (DisplayPositionId)
      references AdviseDisplayPosition (Id)
go

alter table ArticleActionLog
   add constraint FK_ARTICLEA_REFERENCE_ARTICLEI foreign key (ArticleId)
      references ArticleInfo (Id)
go

alter table ArticleActionLog
   add constraint FK_ARTICLEA_REFERENCE_USERS foreign key (UserId)
      references Users (Id)
go

alter table ArticleActionLog
   add constraint FK_ARTICLEA_REFERENCE_ARTICLEA foreign key (ActionTypeId)
      references ArticleActionType (Id)
go

alter table ArticleCommentInfo
   add constraint FK_ARTICLEC_REFERENCE_USERS foreign key (UserId)
      references Users (Id)
go

alter table ArticleCommentInfo
   add constraint FK_ARTICLEC_REFERENCE_ARTICLEI foreign key (ArticleId)
      references ArticleInfo (Id)
go

alter table ArticleHotMappingInfo
   add constraint FK_ARTICLEH_REFERENCE_ARTICLEI foreign key (ArticleId)
      references ArticleInfo (Id)
go

alter table ArticleHotMappingInfo
   add constraint FK_ARTICLEH_REFERENCE_ARTICLEH foreign key (HotTypeId)
      references ArticleHotInfo (Id)
go

alter table ArticleInfo
   add constraint FK_ARTICLEI_REFERENCE_ARTICLET foreign key (TypeId)
      references ArticleTypeInfo (Id)
go

alter table ArticleInfo
   add constraint FK_ARTICLEI_REFERENCE_USERS foreign key (AurthorId)
      references Users (Id)
go

alter table ArticleRewardInfo
   add constraint FK_ARTICLER_REFERENCE_REWARDTY foreign key (RewardTypeId)
      references RewardType (Id)
go

alter table ArticleRewardInfo
   add constraint FK_ARTICLER_REFERENCE_ARTICLEI foreign key (ArticleId)
      references ArticleInfo (Id)
go

alter table ArticleRewardInfo
   add constraint FK_ARTICLER_REFERENCE_USERS foreign key (RewardUserId)
      references Users (Id)
go

alter table FriendLinkInfo
   add constraint FK_FRIENDLI_REFERENCE_WEBSITEI foreign key (WebSiteId)
      references WebSiteInfo (Id)
go

alter table UploadFileInfo
   add constraint FK_UPLOADFI_REFERENCE_WEBSITEI foreign key (WebSiteId)
      references WebSiteInfo (Id)
go

alter table UserRoles
   add constraint FK_USERROLE_REFERENCE_USERS foreign key (UserId)
      references Users (Id)
go

alter table UserRoles
   add constraint FK_USERROLE_REFERENCE_ROLES foreign key (RoleId)
      references Roles (Id)
go
